package com.example.himalayaproject.find.broadcast.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.himalayaproject.R;
import com.example.himalayaproject.find.broadcast.adapter.ProvinceAdapter;
import com.example.himalayaproject.find.broadcast.bean.LocalEntity;
import com.example.himalayaproject.find.broadcast.callback.BroadcastHttpService;
import com.example.himalayaproject.find.broadcast.callback.OnPageLoadListener;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class ProvinceRecyclerFragment extends Fragment implements Callback<LocalEntity>,OnPageLoadListener {
    private BroadcastHttpService service;
    private int pageNum = 1;
    private String provinceCode;
    private ProvinceAdapter adapter;
    private RecyclerView recycler;

    public ProvinceRecyclerFragment() {
        // Required empty public constructor
    }

    public static ProvinceRecyclerFragment newInstance(String provinceCode) {

        Bundle args = new Bundle();
        args.putString("provinceCode",provinceCode);
        ProvinceRecyclerFragment fragment = new ProvinceRecyclerFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_province_recycler, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        provinceCode = getArguments().getString("provinceCode");
        service = new Retrofit.Builder()
                .baseUrl("http://live.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(BroadcastHttpService.class);
        Log.d("1620","provinceFragment:"+provinceCode);
        service.getResult(pageNum,20,provinceCode,2).enqueue(this);
        recycler = ((RecyclerView) view.findViewById(R.id.bct_province_recycler));
        adapter = new ProvinceAdapter(getContext(),this);
        recycler.setAdapter(adapter);

    }

    @Override
    public void onResponse(Call<LocalEntity> call, Response<LocalEntity> response) {
        if (response.body().getResult()!=null){
            adapter.addAll(response.body().getResult());
        }
    }

    @Override
    public void onFailure(Call<LocalEntity> call, Throwable t) {

    }

    @Override
    public void onPageLoad() {
        pageNum++;
        service.getResult(pageNum,20,provinceCode,2).enqueue(this);
    }
}
